package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Login {
	
		private ArrayList<User> users = new ArrayList<User>();
		
		public Login(File file) throws IOException{
			if(file != null){
				FileReader fr = new FileReader(file);
				BufferedReader bf = new BufferedReader(fr);
				String str;
				while((str = bf.readLine()) != null){
					String[] content = str.split(":");
					User user = new User(content[0],content[1]);
					users.add(user);
				}
			}
		}
		
		public void trueLogin(User user) throws IOException{
			if(users.contains(user)){
				checkMoney(user);
				System.out.println(user);
			}
		}
		
		public void checkMoney(User user) throws IOException{
			FileReader fr = new FileReader("save.txt");
			BufferedReader br = new BufferedReader(fr);
			String str = "";
			int count = 0;
			while((str = br.readLine()) != null){
				String[] contents = str.split(":");
				if(contents[0].equals(user.getName())){
					count += Integer.parseInt(contents[1]);
				}
				
			}
			br.close();
			fr.close();
			
			FileReader fr2 = new FileReader("withDraw.txt");
			BufferedReader br2 = new BufferedReader(fr2);
			while((str = br2.readLine()) != null){
				String[] contents = str.split(":");
				if(contents[0].equals(user.getName())){
					count -= Integer.parseInt(contents[1]);
				}
				
			}
			user.setMoney(count);
			br2.close();
			fr2.close();
		}
}
